Skip to content

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Oct 1, 2025

Description

This PR adds virtual module @vitejs/plugin-react/preamble, which allows SSR app to easily enable HMR by adding import side effect at the top of client entry without handling transformIndexHtml. See playground/ssr-react/src/entry-client.jsx for example.

@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Oct 1, 2025

superseded by #891

@hi-ogawa hi-ogawa closed this Oct 1, 2025
@hi-ogawa hi-ogawa deleted the 10-01-feat_expose_virtual_for_simpler_preamble_setup_on_ssr branch October 1, 2025 14:38
@hi-ogawa hi-ogawa restored the 10-01-feat_expose_virtual_for_simpler_preamble_setup_on_ssr branch October 4, 2025 10:31
@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Oct 4, 2025

Okay, I think I want this one instead of #891 as patching react isn't great.

@hi-ogawa hi-ogawa reopened this Oct 4, 2025
hi-ogawa added a commit to hi-ogawa/vite-plugins that referenced this pull request Oct 4, 2025
@hi-ogawa hi-ogawa changed the title feat: expose virtual for simpler preamble setup on ssr feat(react): expose virtual for simpler preamble setup on ssr Oct 6, 2025
hi-ogawa and others added 6 commits October 6, 2025 10:56
- Fix typos: "intiialize" → "initialize", "intiialization" → "initialization"
- Fix grammar: "a following" → "the following"
- Improve wording for clarity and conciseness

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Add preamble section to plugin-react-swc README
- Update CHANGELOG entries for both plugin-react and plugin-react-swc
- Document the new virtual module feature for SSR HMR setup

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@hi-ogawa hi-ogawa marked this pull request as ready for review October 6, 2025 02:39
@hi-ogawa hi-ogawa requested review from sapphi-red and ArnaudBarre and removed request for sapphi-red October 6, 2025 02:45
@sapphi-red
Copy link
Member

If we add support for options.isEntry in dev, I guess we can inject import '@vitejs/plugin-react-swc/preamble' automatically. I think we can have this utility until we have that feature in Vite.

"./refresh-runtime": "./refresh-runtime.js"
},
"dependencies": {
"@rolldown/pluginutils": "1.0.0-beta.41"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be added to SWC deps too (and maybe rsc) because the common package in bundled into react plugins. I'm not a power user of pnpm, but is catalog the best way to unsure it's the same version everywhere?

Also should we use a strict version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this might look weird but I just noticed @rolldown/pluginutils is already in dependencies of all three react packages (and rsc doesn't use it) and they are all pinned with same version. So this should be fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes ok with dependabot this should be done in sync 👍

@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Oct 6, 2025

If we add support for options.isEntry in dev, I guess we can inject import '@vitejs/plugin-react-swc/preamble' automatically. I think we can have this utility until we have that feature in Vite.

That sounds better, but I'm actually not sure how that works. Would that be something like this?

{
  transform: {
    order: 'post',
    handler(code, id) {
      const info = this.getModuleInfo(id);
      if (info?.isEntry) {
         return `import "..";` + code;
      }
    }
  }
}

@sapphi-red
Copy link
Member

If we add support for options.isEntry in dev, I guess we can inject import '@vitejs/plugin-react-swc/preamble' automatically. I think we can have this utility until we have that feature in Vite.

That sounds better, but I'm actually not sure how that works. Would that be something like this?
[omit code]

I was thinking about a code like below as opts.isEntry is available for resolveId.

{
  transform: {
    order: 'post',
    handler(code, id, { isEntry }) {
      if (isEntry) {
         return `import "..";` + code;
      }
    }
  }
}

But turns out opts is not available for transform, so I think it'll be like your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants